package com.ebao.action.user;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.ebao.action.base.BaseAction;
import com.ebao.po.user.TUser;
import com.ebao.po.user.TUserRegister;
import com.ebao.service.user.IUserRegisterService;
import com.ebao.service.user.IUserService;
import com.ebao.util.encrypt.MD5;

/**
 * @author snow
 */
public class UserAction extends BaseAction {

	private static final long serialVersionUID = -4643349923815024953L;
	
	@Resource
	private IUserService userService;
	@Resource
	private IUserRegisterService userRegisterService;
	
	private String id;
	private String encrypt;		// 邮箱中经过MD5加密后的用户邮箱
	
	private String username;
	private String userpass;
	
	
	/**
	 * 用户在邮箱中点击链接，激活账户
	 * 跳转到个人资料设置密码页
	 */
	@Action(value = "confirmRegister", results = {@Result(name = SUCCESS, location = "/index.jsp")})
	public String confirmRegister(){
		TUserRegister UserRegister = userRegisterService.getById(id);
		if(encrypt.equals(MD5.MD5Encode(UserRegister.getEmail()))){
			// 激活账户
			TUser user = new TUser();
			user.setEmail(UserRegister.getEmail());
			user.setUserName(UserRegister.getUserName());
			user.setRegisterDate(UserRegister.getRegisterDate());
			userService.save(user);
		}
		
		return SUCCESS;
	}
	
	private String info;   // for json
	
	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	/**
	 * 用户登录,跳转至个人首页
	 */
	@Action(value = "login", results = {@Result(name = SUCCESS, location = "userhome.jsp"),
			@Result(name = ERROR, type = "json", params = {"root","info"},location = "/index.jsp")})
	public String login(){
		String jpql = "from TUser u where u.userName=? and u.password=?";
		TUser user = userService.getByJpql(jpql, username, userpass);
		if(user != null){
			return SUCCESS;
		} else {
			info = "用户名或密码不匹配";
			return ERROR;
		}
	}
	
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getEncrypt() {
		return encrypt;
	}

	public void setEncrypt(String encrypt) {
		this.encrypt = encrypt;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUserpass() {
		return userpass;
	}

	public void setUserpass(String userpass) {
		this.userpass = userpass;
	}
	
}
